<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SingleSpec;
use App\Models\GoodsSpec;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SingleSpecController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SingleSpec(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('image')->image();
            $grid->column('price');
            $grid->column('cost_price');
            $grid->column('costs_price');
            $grid->column('stock');
            $grid->column('product');
            $grid->column('weight');
            $grid->column('bulk');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->quickCreate(function (Grid\Tools\QuickCreate $create) {
                $create->text('name', '名称');
                $create->email('email', '邮箱');
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SingleSpec(), function (Show $show) {
            $show->field('id');
            $show->field('image');
            $show->field('price');
            $show->field('cost_price');
            $show->field('costs_price');
            $show->field('stock');
            $show->field('product');
            $show->field('weight');
            $show->field('bulk');
            $show->field('created_at');
            $show->field('updated_at');


        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {


        return Form::make(new SingleSpec(), function (Form $form) {

            $form->radio('radio')
                ->when([1], function (Form $form) {
                    $form->display('id');
                    $form->image('image')->uniqueName()->saveFullUrl();
                    $form->text('price');
                    $form->text('cost_price');
                    $form->text('costs_price');
                    $form->text('stock');
                    $form->text('product');
                    $form->text('weight');
                    $form->text('bulk');
                    $form->display('created_at');
                    $form->display('updated_at');
                })
                ->when(2, function (Form $form) {
                    $form->select('','规格名称')->options('spec');
                    $form->display('id');
                    $form->image('image')->uniqueName()->saveFullUrl();
                    $form->text('price');
                    $form->text('cost_price');
                    $form->text('costs_price');
                    $form->text('stock');
                    $form->text('product');
                    $form->text('weight');
                    $form->text('bulk');
                    $form->display('created_at');
                    $form->display('updated_at');
                })
                ->options([
                    1 => '单规格',
                    2 => '多规格',

                ])
                ->default(1);

        });

    }



    /**
     * 下拉菜单展示商品规格
     */
    public function spec(){
         $data = GoodsSpec::select('id','spec_name')->get();
         foreach ($data as $k=>$v){
              $datas[$k]['id']=$v['id'];
              $datas[$k]['text'] =$v['spec_name'];
         }
         return $datas;
    }


}
